import logging.config
from rabbitmqtool import rabbitmq
from redistool import redis
import configparser
import argparse


def make_rabbitmq_config():
    rabbitmq_section = config['DEFAULT']['rabbit_section']
    if group_section_key != '':
        rabbitmq_section = group_section_key
    rabbitmq_section_config = config[rabbitmq_section]
    rabbitmq_config = rabbitmq.RabbitmqConfig(rabbitmq_section_config['server'],
                                              rabbitmq_section_config['port'],
                                              rabbitmq_section_config['username'],
                                              rabbitmq_section_config['password'],
                                              rabbitmq_section_config['vhost'],
                                              rabbitmq_section_config['exchange'],
                                              rabbitmq_section_config['exchange_type'],
                                              rabbitmq_section_config['routing_key'],
                                              rabbitmq_section_config['queue_name'])
    return rabbitmq_config


def make_redis_config():
    default_section = config['DEFAULT']['redis_section']
    if group_section_key != '':
        default_section = group_section_key
    section_config = config[default_section]
    redis_config = redis.RedisConfig(section_config['server'],
                                     section_config['port'],
                                     section_config['db'],
                                     section_config['auth'])
    return redis_config


def rabbitmq_send(msg):
    rabbitmqx = rabbitmq.Rabbitmq(make_rabbitmq_config())
    rabbitmqx.send_msg(msg)


def rabbitmq_consume():
    rabbitmqx = rabbitmq.Rabbitmq(make_rabbitmq_config())
    rabbitmqx.receive_msg()


def redis_send(key, value):
    redisx = redis.Redis(make_redis_config())
    redisx.set(key, value)


def redis_get(key):
    redisx = redis.Redis(make_redis_config())
    redisx.get(key)


logging.config.fileConfig("logger.conf")
logger = logging.getLogger("app")

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Process some services.')
    parser.add_argument("-g", "--group", type=str, default='', help="set group")
    parser.add_argument("-s", "--service", type=str, default='', help="set service")
    parser.add_argument("-m", "--message", type=str, default='', help="set message")
    parser.add_argument("-k", "--key", type=str, default='', help="set key")
    parser.add_argument("-v", "--value", type=str, default='', help="set value")

    args = parser.parse_args()

    group_section_key = args.group

    logger.info("start test...")
    config = configparser.ConfigParser()
    config.read('config.ini')

    if args.service == 'rabbitmq_send':
        rabbitmq_send(args.message)
    elif args.service == 'rabbitmq_consume':
        rabbitmq_consume()
    elif args.service == 'redis_send':
        redis_send(args.key, args.value)
    elif args.service == 'redis_get':
        redis_get(args.key)
    else:
        pass
